import { Module } from '@nestjs/common';
import { FileListService } from './file-list.service';
import { FileListController } from './file-list.controller';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { ConfigModule } from '@nestjs/config';
import { extname, join } from 'path';
import { TypeOrmModule } from '@nestjs/typeorm';
import { FileList } from './entities/file-list.entity';
import * as fs from 'fs';
import { GroupToFlielist } from './entities/grouptofilelist.entity';
import { User } from 'src/user/entities/user.entity';
import { usertogroup } from 'src/group/entities/usertogroup.entity';
import { ImagerasterModule } from 'src/imageraster/imageraster.module';
import { SceneFileRelational } from 'src/scene_file_relational/entities/scene_file_relational.entity';
import * as path from 'path';
@Module({
  imports: [
    MulterModule.register({
      storage: diskStorage({
        destination: function (req, fileName, callback) {
          try {
            const userEmail = (req.user as any).email;
            const destinationPath = path.resolve(
              `${process.env.FILE_PATH}/${userEmail}/temporary`,
            );
            if (!fs.existsSync(destinationPath)) {
              fs.mkdirSync(destinationPath, { recursive: true });
            }
            callback(null, destinationPath);
          } catch (error) {
            console.error(`Error in setting file destination:`, error);
          }
        },
        filename: (_, file, callback) => {
          const fileName = `${
            new Date().getTime() + extname(file.originalname)
          }`;
          return callback(null, fileName);
        },
      }),
    }),
    TypeOrmModule.forFeature([FileList]),
    TypeOrmModule.forFeature([GroupToFlielist]),
    TypeOrmModule.forFeature([User]),
    TypeOrmModule.forFeature([usertogroup]),
    ImagerasterModule,
    ConfigModule.forRoot(),
  ],
  controllers: [FileListController],
  providers: [FileListService],
  exports: [FileListService],
})
export class FileListModule {}
